package com.mes.loco.aps.server.controller.sfc;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.CoreService;
import com.iplant.aps.service.SFCService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSTaskStep;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.ipt.IPTPreCheckProblem;
import com.iplant.aps.service.po.sfc.SFCTaskStep;
import com.iplant.aps.service.po.sfc.SFCTaskStepCar;
import com.iplant.aps.service.po.sfc.SFCTaskStepPart;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.Configuration;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 派工控制器
 *
 * @author PengYouWang
 * 2019年12月26日22:46:00
 */
@RestController
@RequestMapping("/api/SFCTaskStep")
public class SFCTaskStepController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SFCTaskStepController.class);

    final
    SFCService wSFCService;

    final
    CoreService wCoreService;

    public SFCTaskStepController(SFCService wSFCService, CoreService wCoreService) {
        this.wSFCService = wSFCService;
        this.wCoreService = wCoreService;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));

            ServiceResult<SFCTaskStep> wServiceResult = wSFCService.SFC_QueryTaskStep(wLoginUser, wID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 条件查询派工记录
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            int wTaskStepID = StringUtils.parseInt(request.getParameter("TaskStepID"));
            int wShiftID = StringUtils.parseInt(request.getParameter("ShiftID"));
            int wOperatorID = StringUtils.parseInt(request.getParameter("OperatorID"));

            ServiceResult<List<SFCTaskStep>> wServiceResult = wSFCService.SFC_QueryTaskStepList(wLoginUser, wID,
                    wTaskStepID, wShiftID, wOperatorID, -1);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 删除派工记录
     */
    @PostMapping("/DeleteAll")
    public Object DeleteAll(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<SFCTaskStep> wList = CloneTool.CloneArray(wParam.get("data"), SFCTaskStep.class);
            ServiceResult<Integer> wServiceResult = wSFCService.SFC_DeleteList(wLoginUser, wList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 班组长查询某天的派工记录
     */
    @GetMapping("/MonitorRecord")
    public Object MonitorRecord(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            Calendar wShiftDate = StringUtils.parseCalendar(request.getParameter("ShiftDate"));

            ServiceResult<List<SFCTaskStep>> wServiceResult = wSFCService.SFC_QueryTaskStepListByMonitorID(wLoginUser,
                    wShiftDate);

            if (wServiceResult.Result == null)
                wServiceResult.Result = new ArrayList<>();

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 班组成员查询某天的派工任务(打卡的数据源)
     */
    @GetMapping("/EmployeeAll")
    public Object EmployeeAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wTagTypes = StringUtils.parseInt(request.getParameter("TagTypes"));

            ServiceResult<List<SFCTaskStep>> wServiceResult = wSFCService.SFC_QueryTaskStepListByEmployeeID(wLoginUser,
                    wStartTime, wEndTime, wTagTypes);

            if (wServiceResult.Result == null) {
                wServiceResult.Result = new ArrayList<>();
            }
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            if (!wParam.containsKey("data"))
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "参数错误!");

            SFCTaskStep wSFCTaskStep = CloneTool.Clone(wParam.get("data"), SFCTaskStep.class);
            if (wSFCTaskStep == null)
                wResult = GetResult(RetCode.SERVER_CODE_ERR, "参数错误!");

            ServiceResult<Integer> wServiceResult = wSFCService.SFC_UpdateTaskStep(wLoginUser, wSFCTaskStep);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询派工人员列表
     */
    @GetMapping("/PGEmployeeList")
    public Object PGEmployeeList(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = this.GetSession(request);

            int wAPSTaskStepID = StringUtils.parseInt(request.getParameter("APSTaskStepID"));

            ServiceResult<List<BMSEmployee>> wServiceResult = wSFCService.SFC_QueryPGEmployeeList(wLoginUser,
                    wAPSTaskStepID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                ServiceResult<List<BMSEmployee>> wSourceSR = wSFCService.SFC_QueryDispatchEmployeeList(wLoginUser,
                        false, new ArrayList<>(Collections.singletonList(wAPSTaskStepID)));

                //需要去重
                wSourceSR.Result = new ArrayList<BMSEmployee>(wSourceSR.Result.stream()
                        .collect(Collectors.toMap(BMSEmployee::getID, account -> account, (k1, k2) -> k2)).values());

                this.SetResult(wResult, "SourceList", wSourceSR.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 一次性提交派工人员
     */
    @PostMapping("/SubmitAll")
    public Object SubmitAll(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 【派工、全派工】权限控制
            if (!wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 501001, 0, 0)
                    .Info(Boolean.class)
                    && !wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 501002, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            if (!(wParam.containsKey("data"))) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            List<APSTaskStep> wAPSTaskStepList = CloneTool.CloneArray(wParam.get("data"), APSTaskStep.class);

            ServiceResult<Integer> wServiceResult = wSFCService.SFC_TaskStepSubmitAllNew_v3(wLoginUser,
                    wAPSTaskStepList);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 添加派工人员(开工状态下)
     */
    @PostMapping("/AddPerson")
    public Object AddPerson(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            APSTaskStep wData = CloneTool.Clone(wParam.get("data"), APSTaskStep.class);
            List<Integer> wPersonIDList = CloneTool.CloneArray(wParam.get("PersonIDList"), Integer.class);

            ServiceResult<Integer> wServiceResult = wSFCService.SFC_AddPerson(wLoginUser, wData, wPersonIDList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 保存派工人员
     */
    @PostMapping("/SaveAll")
    public Object SaveAll(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (this.CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            if (!(wParam.containsKey("data"))) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            List<APSTaskStep> wAPSTaskStepList = CloneTool.CloneArray(wParam.get("data"), APSTaskStep.class);

            ServiceResult<Integer> wServiceResult = wSFCService.SFC_TaskStepSaveAll(wLoginUser, wAPSTaskStepList);
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 班组长调整派工任务的工时
     */
    @PostMapping("/AdjustHour")
    public Object AdjustHour(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<SFCTaskStep> wSFCTaskStepList = CloneTool.CloneArray(wParam.get("SFCTaskStepList"), SFCTaskStep.class);
            Double wHour = StringUtils.parseDouble(wParam.get("Hour"));

            if (wSFCTaskStepList == null || wSFCTaskStepList.size() <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            if (!wSFCTaskStepList.stream().allMatch(p -> p.MonitorID == wLoginUser.ID)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            ServiceResult<Integer> wServiceResult = wSFCService.SFC_AdjustHour(wLoginUser, wSFCTaskStepList, wHour);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 问题项派工
     */
    @PostMapping("/SubmitPGProblems")
    public Object SubmitPGProblems(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 权限控制
            if (Configuration.readConfigString("EnableAccessControl", "config/config").equals("1")
                    && !wCoreService.BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID,
                            1502002, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            List<IPTPreCheckProblem> wDataList = CloneTool.CloneArray(wParam.get("data"), IPTPreCheckProblem.class);
            if (wDataList == null || wDataList.size() <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<Integer> wServiceResult = wSFCService.SFC_SubmitPGProblems(wLoginUser, wDataList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    @PostMapping({"/TaskAllByTaskStepIDList"})
    public Object TaskAllByTaskStepIDList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            List<Integer> wTaskStepIDList = CloneTool.CloneArray(wParam.get("data"), Integer.class);
            if (wTaskStepIDList == null || wTaskStepIDList.size() <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<List<SFCTaskStep>> wServiceResult = this.wSFCService.SFC_QueryTaskStepList(wLoginUser,
                    wTaskStepIDList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 删除重复的派工记录
     */
    @GetMapping("/DeleteRepeat")
    public Object PreCheckTaskAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = wSFCService.SFC_DeleteRepeat(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "删除成功", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取派工车辆列表
     */
    @GetMapping("/DispatchCarList")
    public Object DispatchCarList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<List<SFCTaskStepCar>> wServiceResult = wSFCService.SFC_QueryDispatchCarList(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取派工工位列表
     */
    @GetMapping("/DispatchPartList")
    public Object DispatchPartList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));

            if (wOrderID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<SFCTaskStepPart>> wServiceResult = wSFCService.SFC_QueryDispatchPartList(wLoginUser,
                    wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
